package com.example.demo1.temp;

import org.junit.Test;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.function.Consumer;

/**
 * @author tang
 */
public class TestLambda2 {
    @Test
    public void test1(){
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("java 7");
            }
        };
        r1.run();
        Runnable r2 = ()-> System.out.println("java8");
        r2.run();
    }

    @Test
    public void test2(){
        Consumer<String> c1 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        c1.accept("原始有参数Java8");

        Consumer<String> c = (x)-> System.out.println(x);
        c.accept("java8 有参数");
    }

    @Test
    public void test3(){
       Comparator<Integer> cp = (x,y)->{
          System.out.println("函数式接口");
          return Integer.compare(x,y);
        };
        System.out.println("Lambda体中多条语句"+cp.compare(1,12));
    }
    @Test
    public void test4(){
        Comparator<Integer> cp = (x,y)-> Integer.compare(x,y);
        System.out.println("Lambda体中单条语句"+cp.compare(1,12));
    }
    @Test
    public void test5(){
        Comparator<Integer> cp = (Integer x,Integer y)-> Integer.compare(x,y);
    }

    @Test
    public void test6(){
        System.out.println("结果是"+oprator(3,5,(x,y)-> BigDecimal.valueOf(x+y)));
    }

    public BigDecimal oprator(int x,int y,MyMath myMath){
        return myMath.oprater(x,y);
    }
}
